<template>
	<view class="modal">
		<view class="modal-bkg" />
		<view class="modal-pop">
			<view class="close" @tap="close">
				<img src="@/static/icon/icon_16_close_w@2x.png" alt="">
			</view>
			<view class="pop-main">
				<view class="title">绑定抖音</view>
				<view class="main">
					<input type="text" @input="inputChange" :value="douyinnum" placeholder="请输入您的抖音号">
					<view class="input-desc">
						*抖音号绑定后将不可变更，请输入正确的本人抖音号，恶意绑定让人抖音号将会受到封号惩罚并取消所有中奖纪录
					</view>
				</view>
				<view class="next-btn" @click="bindDouyin">
					下一步
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {mapState} from 'vuex'
	export default {
		name: 'wx-auth',
		props: {
			authType: {
				type: Array,
				default(){
					return ['authAvatar']
				}
			},
			checkItem:{
				type: Object,
				default(){
					return {}
				}
			}
		},
		computed:{
			...mapState(['userinfo'])
		},
		data() {
			return {
				douyinnum: null
			};
		},
		methods: {
			inputChange(event){
				console.log(event.target.value)
				let value = event.target.value
				console.log('value',value)
				let newVal = value.replace('抖音搜索','').replace('复制全文就能搜','').trim()
				console.log(newVal)
				this.douyinnum = newVal
			},
			close(){
				console.log('关闭了')
				uni.$emit('pop_close')
			},
			bindDouyin(){
				if(!this.douyinnum){
					uni.showToast({
						title: "请填入您的抖音号",
						icon: "none"
					})
				}else{
					uni.showModal({
					    title: '',
					    content: '抖音号绑定完成后将不可修改',
						confirmText: '确认绑定',
					    success:  (res) =>{
					        if (res.confirm) {
					            this.$api.doTask({
					            	url: this.douyinnum,
					            	task_id: this.checkItem.id
					            }).then(res => {
									let {status} = res
									if(status == 1){
										uni.showToast({
											title: "绑定抖音号成功，请开始任务",
											icon: "none"
										})
										setTimeout(()=> {
											this.$store.dispatch("set_authflag", {
												bindDouyin: true, 
											})
											this.close()
										}, 1500)
									}else if(status == 2){
										uni.showToast({
											icon: 'none',
											title: '该抖音号已被绑定'
										})
									}else {
										uni.showToast({
											title: "绑定抖音号失败，请重试",
											icon: "none"
										})
									}
					            })
					        } else if (res.cancel) {
					            console.log('用户点击取消');
					        }
					    }
					});
					
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.modal{
		position: fixed;
		top: 0;
		.modal-bkg{
			position: fixed;
			top: 0;
			background-color: rgba(0,0,0,.5);
			width: 100vw;
			height: 100vh;
		}
		.modal-pop{
			position: absolute;
			top: calc(50vh - 146px);
			left: calc(50vw - 160px);
			width: 320px;
			height: 292px;
			background-image: url('@/static/index/img_modal_card_01@2x.png');
			background-size: 320px 292px;
			.close{
				position: absolute;
				top: -56px;
				right: 0;
				width: 72px;
				height: 56px;
				background: url('@/static/btn/btn_32_ghost_shadow_nor@2x.png');
				background-size: 72px 56px;
				display: flex;
				align-items: center;
				justify-content: center;
				img{
					width: 15px;
					height: 15px;
				}
			}
			.pop-main{
				width: 248px;
				margin: 26px auto;
				.title{
					width: 64px;
					height: 22px;
					font-size: 16px;
					color: #F82045;
					font-weight: 600;
				}
				.main{
					input{
						width: 248px;
						height: 48px;
						background: #F5F5F5;
						border-radius: 8px;
						padding-left: 10px;
						padding-right: 10px;
						margin-top: 40px;
					}
					.input-desc{
						height: 58px;
						background: #FDF8DF;
						border-radius: 8px;
						padding: 8px 12px;
						font-size: 10px;
						color: #805340;
						margin-top: 12px;
					}
				}
				.next-btn{
					font-weight: 500;
					width: 152px;
					height: 56px;
					margin: 22px auto 0;
					text-align: center;
					line-height: 56px;
					background: url('@/static/btn/btn_32_red_152_shadow_nor@2x.png');
					background-size: 152px 56px;
					background-repeat: no-repeat;
					color: #FFFFFF;
				}
			}
			
		}
		
	}
</style>
